package com.koi.plus.config;

import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author ･ᴗ･
 * @description [分页配置和自动填充]
 */
@Configuration
@EnableTransactionManagement
// 多包名使用 例如 com.koi.**.mapper,com.xxx.**.mapper
@MapperScan("com.koi.**.mapper")
public class MyBatisPlusConfiguration {

    /**
     * @description [MybatisPlus拦截器]
     * @author ･ᴗ･
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * @description [自动填充创建时间和修改时间]
     * @author ･ᴗ･
     */
    @Bean
    public CommonMetaObjectHandler commonMetaObjectHandler() {
        return new CommonMetaObjectHandler();
    }

    /**
     * @description [乐观锁插件]
     * @author ･ᴗ･
     */
    @Bean
    public OptimisticLockerInnerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 使用网卡信息绑定雪花生成器
     * 防止集群雪花ID重复
     */
    @Bean
    public IdentifierGenerator idGenerator() {
        return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
    }

    /**
     * @description [逻辑删除插件-yml进行配置过，这里不进行配置]
     * @author ･ᴗ･
     *
     * @TableLogic(value = "N",delval = "Y")
     * @TableLogic(value = "0",delval = "1")
     */
    // @Bean
    // public GlobalConfig globalConfig() {
    //     GlobalConfig globalConfig = new GlobalConfig();
    //     GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
    //     dbConfig.setLogicDeleteValue("0");
    //     dbConfig.setLogicNotDeleteValue("1");
    //     globalConfig.setDbConfig(dbConfig);
    //     return globalConfig;
    // }

    /**
     * @description [乐观锁插件]
     * @author ･ᴗ･
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * @description [分页插件，自动识别数据库类型]
     * @author ･ᴗ･
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOptimizeJoin(true);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInnerInterceptor.setMaxLimit(500L);
        paginationInnerInterceptor.setOverflow(false);
        // 如果配置多个插件，切记分页最后添加
        // paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // paginationInnerInterceptor.setDbType(DbType.ORACLE);
        // 如果有多数据源可以不配具体类型，否则都建议配上具体的DbType[本项目已经使用多数据源，无需进行上方注释的配置]
        return paginationInnerInterceptor;
    }

}
